package com.xiaoqian.partyembassy.ui.fragment.home;


import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.xiaoqian.partyembassy.R;
import com.xiaoqian.partyembassy.adapter.HomeCommodityAdpter;
import com.xiaoqian.partyembassy.base.BaseFragment;
import com.xiaoqian.partyembassy.constant.UrlConfig;
import com.xiaoqian.partyembassy.tools.CommonToolUtils;
import com.xiaoqian.partyembassy.tools.okhttp.CallBackUtil;
import com.xiaoqian.partyembassy.tools.okhttp.OkhttpUtil;
import com.xiaoqian.partyembassy.ui.activity.AllExhibitsActivity;
import com.xiaoqian.partyembassy.ui.activity.SearchActivity;
import com.xiaoqian.partyembassy.view.NumberBgPicker;
import com.xiaoqian.partyembassy.view.NumberPicker;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;


public class HomeFragment extends BaseFragment implements NumberPicker.OnValueChangeListener {
    private ListView mListview;
    // 当前选择楼层位置
    private int mCurrentFloorValue = 0;
    // 当前选择房间位置
    private int mCurrentRoomValue = 0;
    private NumberBgPicker floorPicker;
    private NumberPicker roomPicker;
    private NumberPicker commoditydetails_picker;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initParams() {

    }

    @Override
    protected void initView() {
        mListview = view.findViewById(R.id.fragment_home_lv);
        LayoutInflater inflater = getLayoutInflater();
        addHeadView(inflater);
        addFootView(inflater);
        List<String> mList = new ArrayList<>();
        mList.add("1");
        mList.add("1");
        mList.add("1");
        HomeCommodityAdpter homeCommodityAdpter = new HomeCommodityAdpter(mContext,mList);
        mListview.setAdapter(homeCommodityAdpter);
        getData();
    }
    private void getData(){
        Map<String,String> paramsMap = new HashMap<>();
        paramsMap.put("indoor-uid","0");
        paramsMap.put("indoor-token","1234567890123456");
        OkhttpUtil.okHttpPost(UrlConfig.getShowroom, paramsMap, new CallBackUtil.CallBackString() {

            @Override
            public void onFailure(Call call, Exception e) {
                Log.e("onFailure",e.toString()+"-");
            }

            @Override
            public void onResponse(String response) {
                Log.e("onFailure",response+"-");
                JSONObject json = CommonToolUtils.getJSON(response);
                if(null != json){

                }
            }
        });
    }
    private void addFootView( LayoutInflater inflater) {
        View footView = inflater.inflate(R.layout.home_item_foot_view, null);
        footView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //查看所有
                startActivity(AllExhibitsActivity.class);
            }
        });
        mListview.addFooterView(footView);
    }

    private void addHeadView( LayoutInflater inflater){
        View headView = inflater.inflate(R.layout.home_item_head_view, null);
        floorPicker = headView.findViewById(R.id.floorPicker);
        roomPicker = headView.findViewById(R.id.roomPicker);
        commoditydetails_picker = headView.findViewById(R.id.commoditydetails_picker);
        headView.findViewById(R.id.search_position_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(SearchActivity.class);
            }
        });
        initFloorPickerData();
        floorPicker.setOnValueChangedListener(new NumberBgPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberBgPicker picker, int oldVal, int newVal, EditText editText) {
                switch (picker.getId()) {
                    case R.id.floorPicker:
                        mCurrentFloorValue = newVal;
                        //重新获取房间列表
                        break;
                }
            }
        });
        headView.findViewById(R.id.search_position_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(SearchActivity.class);
            }
        });
        roomPicker.setOnValueChangedListener(this);
        commoditydetails_picker.setOnValueChangedListener(this);
        mListview.addHeaderView(headView);

    }
    /**
     * 初始化楼层数据
     */
    private void initFloorPickerData() {

        mCurrentFloorValue = 0;
        String[] nameArr = getFloorNameArray();
        if (nameArr.length > 0) {
            // 设置灯关面板picker
            floorPicker.setMaxValue(nameArr.length - 1);
            floorPicker.setMinValue(0);
            floorPicker.setDisplayedValues(nameArr);
            floorPicker
                    .setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        }

    }
    private String[] floorArray = null;
    /**
     * 获取楼层名称列表
     */
    private String[] getFloorNameArray() {
        floorArray = new String[5];
        for (int i = 0; i < 5; i++) {
            String floorName = i+"";
            if (TextUtils.isEmpty(floorName)) {
                floorArray[i] = "默认楼层";
            } else {
                floorArray[i] = floorName;
            }
        }
        return floorArray;
    }
    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal, EditText editText) {
        switch (picker.getId()) {
            case R.id.roomPicker:
                mCurrentRoomValue = newVal;
                break;
        }
    }
}